package com.csun.dsms.controller.process;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.mapper.FileDirectoryMapper;
import com.csun.dsms.mapper.FileInfoMapper;
import com.csun.dsms.model.process.FileDirectory;
import com.csun.dsms.model.process.FileInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;
import java.util.*;

@RestController
@RequestMapping("/fileData")
public class FileDataController {

    @Autowired
    private FileDirectoryMapper fileDirectoryMapper;

    @Autowired
    private FileInfoMapper fileInfoMapper;

    @RequestMapping("/addFileDirectory")
    public Wrapper<Object> addLearnInfo(@RequestBody FileDirectory fileDirectory) {
       try{
           String directoryName = fileDirectory.getDirectoryName();
           if(StrUtil.isBlank(directoryName)){
               return WrapMapper.wrap(Wrapper.ERROR_CODE, "目录名称不能为空");
           }
           FileDirectory newFileDirectory = new FileDirectory();
           newFileDirectory.setDirectoryName(directoryName);
           FileDirectory oldFileDirectory = fileDirectoryMapper.selectOne(newFileDirectory);
           if(oldFileDirectory != null){
               return WrapMapper.wrap(Wrapper.ERROR_CODE, "目录已存在");
           }
           fileDirectoryMapper.insert(fileDirectory);
           return WrapMapper.ok();
       }catch (Exception e){
           e.printStackTrace();
           return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
       }
    }

    @RequestMapping("/getFileDirectory")
    public Wrapper<Object> getFileDirectory(@RequestBody FileDirectory fileDirectory) {
        try{
            BigInteger id = fileDirectory.getId();
            if(id==null){
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "id不能为空");
            }
            List<FileDirectory> fileDirectoryList = fileDirectoryMapper.select(fileDirectory);
            return WrapMapper.ok(fileDirectoryList);
        }catch (Exception e){
            e.printStackTrace();
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }
    @RequestMapping("/getFileDirectoryAll")
    public Wrapper<Object> getFileDirectoryAll(@RequestBody FileDirectory fileDirectory) {
        try {
            Integer mainId = fileDirectory.getMainId();
            if(mainId==null){
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "mainId不能为空");
            }
            List<FileDirectory> fileDirectoryList = fileDirectoryMapper.select(fileDirectory);
            if(CollectionUtil.isEmpty(fileDirectoryList)){
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "目录不存在");
            }

            List<FileDirectory> tree = new ArrayList<>();
            Map<String, FileDirectory> map = new HashMap<>();
            // 先将所有节点放入 map 中
            for (FileDirectory node : fileDirectoryList) {
                map.put(Convert.toStr(node.getId()), node);
            }
            // 构建树形结构
            for (FileDirectory node : fileDirectoryList) {
                String parentCode = Convert.toStr(node.getParentId());
                if (parentCode == null || !map.containsKey(parentCode)) {
                    // 根节点
                    tree.add(node);
                } else {
                    // 非根节点，添加到父节点的 children 列表中
                    FileDirectory parent = map.get(parentCode);
                    if (parent.getChildren() == null) {
                        parent.setChildren(new ArrayList<>());
                    }
                    parent.getChildren().add(node);
                }
            }
            List<JSONObject> resList = new ArrayList<>();
            for (FileDirectory node : tree) {
                JSONObject json = convertToJsonByFileDirectory(node);
                resList.add(json);
            }
            return WrapMapper.ok(resList);
        } catch (Exception e) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }


    @RequestMapping("/addFileInfo")
    public Wrapper<Object> addFileInfo(@RequestBody FileInfo addFileInfo) {
        try{
            BigInteger directoryId = addFileInfo.getDirectoryId();
            String filePath = addFileInfo.getFilePath();
            if(directoryId==null){
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "目录id不能为空");
            }
            if(StrUtil.isBlank(filePath)){
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "文件路径不能为空");
            }
            fileInfoMapper.insert(addFileInfo);
            return WrapMapper.ok();
        }catch (Exception e){
            e.printStackTrace();
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }

    private JSONObject convertToJsonByFileDirectory(FileDirectory node) {
        JSONObject json = new JSONObject();
        json.put("directoryId", node.getId());
        json.put("directoryName", node.getDirectoryName());
        FileInfo fileInfo = new FileInfo();
        fileInfo.setDirectoryId(node.getId());
        json.put("fileInfoList", fileInfoMapper.select(fileInfo));
        List<FileDirectory> children = node.getChildren();
        if (children != null && !children.isEmpty()) {
            List<JSONObject> childrenJson = new ArrayList<>();
            for (FileDirectory child : children) {
                childrenJson.add(convertToJsonByFileDirectory(child));
            }
            json.put("childType", childrenJson);
        }
        return json;
    }
}
